package com.psdi.manager;

import com.psdi.element.*;
import com.psdi.physics.Collider;
import com.psdi.physics.CollisionSystem;

import javax.swing.*;
import java.io.*;
import java.util.*;

/**
 * 游戏加载器类
 * 用于加载游戏资源和配置
 */
public class GameLoader {
    private static final ElementManager elementManager= ElementManager.getINSTANCE();
    public static Map<Vector2D, List<ImageIcon>> iconMap= new HashMap<>();
    public static Map<String,List<ImageIcon>> iconMap2 = new HashMap<>();
    public static Map<String, ImageIcon> wallIconMap;
    public static Map<Vector2D, ImageIcon> enemyIconMap;
    public static ImageIcon PropIcon;//道具gif
    static {
          // 数量较少在此处加载
        wallIconMap = new HashMap<>();
        wallIconMap.put("MAP1", new ImageIcon("resource/map/map1/map1.png"));
        wallIconMap.put("MAP2", new ImageIcon("resource/map/map1/map2.png"));
        enemyIconMap = new HashMap<>();
        enemyIconMap.put(Vector2D.LEFT, new ImageIcon("resource/image/tank/bot/bot_left.png"));
        enemyIconMap.put(Vector2D.RIGHT, new ImageIcon("resource/image/tank/bot/bot_right.png"));
        enemyIconMap.put(Vector2D.UP, new ImageIcon("resource/image/tank/bot/bot_up.png"));
        enemyIconMap.put(Vector2D.DOWN, new ImageIcon("resource/image/tank/bot/bot_down.png"));
        enemyIconMap.put(Vector2D.ZERO, new ImageIcon("resource/image/tank/bot/bot_left.png"));

        PropIcon = new ImageIcon("resource/image/Prop/Prop1.png");
    }
    private static final Properties properties = new Properties();
    public static void mapLoad(String mapId) {
//        String mapFileName = "resource/map/data/" + mapId + ".map";
//        properties.clear();
//        try {
//            BufferedReader bufferedReader=new BufferedReader(new FileReader(mapFileName));
//            properties.load(bufferedReader);
//            Set<String> keys = properties.stringPropertyNames();
//            for (String key : keys) {
//                String value = properties.getProperty(key);
//                for (String position : value.split(";")) {
                    ElementObject mapObject = new MapObject().createMapObject(mapId,"0,0");
                    elementManager.addElement(mapObject);
//                }
//            }
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
    }
    public static void LoadImg(){
        String texturl = "resource/playerImg/playerimage"; //图片文档，暂时使用该名字
        String[] stringList = new String[]{"0,1","1,0","-1,0","0,-1"};
        Set<String> validKeys = new HashSet<>(Arrays.asList(stringList));
        //Img用于存放数据
        properties.clear();
        try{
            BufferedReader reader = new BufferedReader(new FileReader(texturl));
            properties.load(reader);
            Set<String> keys = properties.stringPropertyNames();
            for (String key : keys) {
                List<ImageIcon> imageIcons = new ArrayList<>();
                String value = properties.getProperty(key);
                for (String imagepath : value.split(";")) {
                    imageIcons.add(new ImageIcon(imagepath));
                }
                /*
                  通过判断条件实现多种类的图片存入对应集合
                 */
                if(validKeys.contains(key)){
                    iconMap.put(Vector2D.parseVector2D(key),imageIcons);
                }
                else{
                    iconMap2.put(key,imageIcons);
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void LoadPlay(){
        ElementObject play = new Player().CreatPlayer();
        elementManager.addElement(play);
    }

    public static void LoadProp(Position pos){
        ElementObject prop = new Prop().CreateProp(pos);
        elementManager.addElement(prop);
    }
    public static void LoadDoor(Position pos){
        ElementObject door = new Door().CreateDoor(pos);
        elementManager.addElement(door);
    }

    public static void LoadEnemy(Position pos,float num,float speed){
        ElementObject enemy = new Enemy().CreateEnemy(pos,num,speed);
        elementManager.addElement(enemy);
    }
    public static void LoadBoom(Position pos, Size size){
        ElementObject boom = new Boom().CreateBoom(pos,size);
        elementManager.addElement(boom);
    }
    public static void LoadLevel(){
        ElementObject level = new Level().CreateLevel();
        ElementObject blood = new Blood().CreateBlood();
        elementManager.addElement(level);
        elementManager.addElement(blood);
    }
    public static void LoadKnifeairflow(Position pos,Vector2D Direction){
        ElementObject knifeairflow = new KnifeAirflow().CreatKnifeAirflow(pos,Direction);
        CollisionSystem.INSTANCE.addElement((Collider) knifeairflow);
        elementManager.addElement(knifeairflow);

    }
    public static void LoadBoss(Position pos,float num,Size size){
        ElementObject boss = new Boss().creatBoss(pos,num,size);
        elementManager.addElement(boss);
    }
    public static void LoadBossBullet(Position pos,Vector2D Direction,Size size){
        ElementObject bossbullet = new BossBullet().CreatBossBullet(pos,Direction,size);
        elementManager.addElement(bossbullet);
    }
    public static void LoadEnd(boolean victory){
        ElementObject end = new End().creat(victory);
        elementManager.addElement(end);
    }
    public static void LoadBullet(String string){
        ElementObject elementObject=new Bullet().create(string);
        CollisionSystem.INSTANCE.addElement((Collider) elementObject);
        ElementManager.getINSTANCE().addElement(elementObject);
    }
    public static void LoadKnifeairflow(String string){
        ElementObject obj = new Knife().create(string);
        CollisionSystem.INSTANCE.addElement((Collider) obj);
        ElementManager.getINSTANCE().addElement(obj);

    }
}
